package com.dau.system.config.rabbit;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dau.common.util.GlobalConstants;
import com.dau.entity.customer.ItemInfo;
import com.dau.entity.customer.OrderInfo;
import com.dau.service.customer.item.ItemService;
import com.dau.service.customer.order.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@Component
@Slf4j
public class RabbitReceiver implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    /**
     * 接收死信消息
     *
     * @param message 消息
     * @param channel 通道
     * @throws IOException IOException
     */
    @RabbitListener(queues = "receive.queue.name")
    public void receiveDead(Message message, Channel channel) throws IOException {
        OrderInfo orderInfo = JSONUtil.toBean(new String(message.getBody()), OrderInfo.class);
        System.out.println("收到死信消息：" + JSONUtil.toJsonStr(orderInfo));
        OrderService orderService = (OrderService) applicationContext.getBean("orderService");
        ItemService itemService = (ItemService) applicationContext.getBean("itemService");
        OrderInfo storesData = orderService.getById(orderInfo.getOrderId());
        if (storesData != null && GlobalConstants.ZERO_STR.equals(storesData.getStatus())) {
            orderService.removeById(orderInfo);

            List<ItemInfo> list = itemService.list(new QueryWrapper<ItemInfo>().eq("order_id", orderInfo.getOrderId()));
            if (CollectionUtils.isNotEmpty(list)) {
                for (ItemInfo itemInfo : list) {
                    itemService.removeById(itemInfo.getItemId());
                }
            }
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * 直接队列
     *
     * @param testMessage 测试消息
     */
    @RabbitListener(queues = "NewDirectQueue")
    public void newDirectQueue(Map testMessage) {
        System.out.println("DirectReceiver消费者收到消息  : " + testMessage.toString());
    }

    /**
     * top order
     *
     * @param testMessage 测试消息
     */
    @RabbitListener(queues = "topic.order")
    public void topicOrder(String testMessage) {
        System.out.println("TopicOrderReceiver消费者收到消息  : " + testMessage);
    }


    /**
     * top  user
     *
     * @param testMessage 测试消息
     */
    @RabbitListener(queues = "topic.user")
    public void process(String testMessage) {
        System.out.println("TopicUserReceiver消费者收到消息  : " + testMessage);
    }

    /**
     * 分发 A
     *
     * @param testMessage 测试消息
     */
    @RabbitListener(queues = "fanout.A")
    public void fanoutA(String testMessage) {
        System.out.println("FanoutReceiverA消费者收到消息  : " + testMessage);
    }

    /**
     * 分发 B
     *
     * @param testMessage 测试消息
     */
    @RabbitListener(queues = "fanout.B")
    public void fanoutB(String testMessage) {
        System.out.println("FanoutReceiverB消费者收到消息  : " + testMessage);
    }


    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
